package com.liujun.security.security;

import com.liujun.security.utils.RsaUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author pc
 */
@ConfigurationProperties(prefix = "rsa.key")
@Data
@Component
@Slf4j
public class RsaKeyProperties {
    private String publicKeyFile;
    private String privateKeyFile;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    @PostConstruct
    public void createRsaKey() throws Exception {

        if (!new File(publicKeyFile).exists() && !new File(privateKeyFile).exists()) {
            log.info("rsa密钥不存在,===>生成密钥");
            RsaUtil.generateKey(publicKeyFile, privateKeyFile, "security", 0);
        }
        publicKey = RsaUtil.getPublicKey(publicKeyFile);
        privateKey = RsaUtil.getPrivateKey(privateKeyFile);
    }
}


